package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;

public class Lake extends _ArticleRule {

	public Pattern TheLake = Pattern.compile("\\bCahora Bassa\\b|\\bGreat Prespa\\b|\\bSmall Prespa\\b|\\bGreat Salt\\b|\\bGevena\\b");
	public Pattern TheNoLake = Pattern.compile("\\bMakgadikgadi Pan\\b|\\bCaspian Sea\\b");
	public Pattern NoTheNoLake = Pattern.compile("\\bPangong Tso\\b|\\bChandra Taal\\b|\\bChandra Tal\\b|\\bSuraj Tal\\b|\\bSurya taal\\b|\\bTsomoriri\\b|\\bGavkhouni\\b|\\bAla Köl\\b|\\bAla-Kul\\b|\\bIssyk Kul\\b|\\bYsyk Köl\\b|\\bIssyk-Kol\\b|\\bJashyl Kel\\b|\\bKel-Ukek\\b|\\bKöl-Suu\\b|\\bKylaa-Kel\\b|\\bSary-Kel\\b|\\bLago di Lei\\b|\\bEstanys de Baiau\\b|\\bEl Atazar Dam\\b|\\bEmbalse de Navacerrada\\b");

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if ( NP.depth() != 2) return Rule.Inapplicable;//如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if ( "NN".equals(headword.tag()) || "NNP".equals(headword.tag())) {
			return Rule.Applicable;//如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		/*
		 * 名词短语中
		 */
		sb.clear();
		for( TaggedWord tw : NP.taggedYield()) {
			sb.append(tw.word()).append(" ");
		}
		
		if ( sb.toString().trim().contains("Lake") ||  sb.toString().trim().contains("lake")) {
			if (  sb.toString().trim().startsWith("the") || sb.toString().trim().startsWith("The")) {
				if ( this.TheLake.matcher(sb.toString().trim()).find()) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;						
				}
			} else {
				return null;
			}
		} else {
			if (  sb.toString().trim().startsWith("the") || sb.toString().trim().startsWith("The")) {
				if ( this.TheNoLake.matcher(sb.toString().trim()).find()) {
					return null;
				} else {
					return Rule.Inapplicable;
				}
			} else {
				if ( this.NoTheNoLake.matcher(sb.toString().trim()).find()) {
					return null;
				} else {
					return Rule.Inapplicable;
				}
			}			
		}

	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("湖名一般前面不加定冠词，也有个别添加定冠词的。比如：the Lake of Geneva，the Great Salt Lake。");
		sb.append("</p>");			
		return sb.toString();
	}
}
